App.MainApp = angular.module('MainApp', ['Error', 'Tools', 'Template', 'User', 'Note', 'NotesGroup']).
controller('MainApp.Ctrl', ['$scope', 'MainApp.DataService', 'MainApp.ConfigService', 'Tools.Service', '$rootScope', 'MainApp.EventHandlersService', function ($scope, Data, Config, Tools, $rootScope, EventHandlers) {
	Data.Page.actual = Config.Template.defaultPage;
	$scope.Config = Config;
	$scope.Data = Data;
	$scope.actualUser;
	$scope.actualNote;
	$scope.$on('DialogBox.show', EventHandlers.DialogBox.show);
	$scope.$on('Error.catch', EventHandlers.Error.catch);
	$scope.$on('User.login', EventHandlers.User.login);
	$scope.$on('Page.show', EventHandlers.Page.show);
	$scope.$on('App.refreshState', EventHandlers.App.refreshState);
	$scope.$on('Note.setActual', EventHandlers.Note.setActual);
	$scope.refreshActualUserData = function () {
		if(Data.User.actualUser) {
			$scope.actualUser = Data.User.actualUser.toObject();
		}
	}
	$scope.refreshActualNote = function () {
		$scope.actualNote = Data.Note.actualNote.toObject();
	}
}]).
service('MainApp.ConfigService', [function () {
	this.Template = {
		templateDir: 'html/',
		pageDir: 'html/pages/',
		defaultPage: 'login',
		directiveExt: '.html',
		Page: {
			login: 'login',
			notes: 'notes'
		}
	}
	this.Api = {
		address: 'http://tales.ms.polsl.pl/interfejsy/notatki/?',
		cleanValue: 'CLEAN',
		validResponseValue: 'ok',
		emptyItem: ''
	}
	this.User = {
		Admin: {
			login: 'superadmin',
			password: '123456'
		}
	}
	this.Error = {
		DefaultError: {
			type: 'error',
			message: 'Wystąpił wewnętrzny błąd aplikacji uniemożliwiający wykonanie zadanie. Proszę spróbować później.'
		}
	}
	this.Note = {
		emptyNote: {
			title: '',
			intro: '',
			content: ''
		}
	}
}]).
service('MainApp.DataService', [function () {
	this.Page = {
		actual: false
	}
	this.Footer = {
		content: 'Projekt © 2014'
	}
	this.Dialog = {
		type: 'empty',
		message: 'ala ma kota',
		show: false
	}
	this.User = {
		actualUser: false
	}
	this.Note = {
		actualNote: false
	}
}]).
service('MainApp.EventHandlersService', ['MainApp.DataService', '$rootScope', 'MainApp.ConfigService', 'Tools.Service', function (Data, $rootScope, Config, Tools) {
	this.App = {
		refreshState: function (event) {
			event.currentScope.refreshActualUserData();
			event.currentScope.refreshActualNote();
		}
	}
	this.DialogBox = {
		show: function (event, obj) {
			Data.Dialog = {
				type: obj.type,
				message: obj.message,
				show: true
			}
		}
	}
	this.Error = {
		catch: function (event, error) {
			$rootScope.$broadcast('DialogBox.show', {
				type: 'error', 
				message: error.getMessage()
			});
		}
	}
	this.User = {
		login: function (event, user) {
			Data.User.actualUser = user;
			user.downloadData().then(
				function () {
					$rootScope.$broadcast('App.refreshState');
					$rootScope.$broadcast('Page.show', Config.Template.Page.notes);
				},
				function (error) {
					$rootScope.$broadcast('Error.catch', error);
				}
			);
		}
	}
	this.Page = {
		show: function (event, name) {
			Data.Page.actual = Config.Template.Page[name];
		}
	}
	this.Note = {
		setActual: function (event, note) {
			Data.Note.actualNote = Tools.inject('Note.Factory').fromObject(note);
			$rootScope.$broadcast('App.refreshState');
		}
	}
}])

